<?php

class Taxi_Model_AuthTaxistes extends Zend_Db_Table_Abstract
{
    protected $_name = 'auth_taxistes';
    protected $_primary = 'mail';
    
    public function insert_token ($mail){
        $token = rand(1,1000000);
        
        $select = $this->_db->select()
                    ->from($this->_name)
                    ->where('mail = ?', $mail);
        
        $result = $select->query()->fetchAll();
        
        if (sizeof($result) == 0){
            $data = array(
            'mail'  =>  $mail,
            'auth'  =>  $token
            );
        
            $this->insert($data);
            
        }else {
            $data = array('auth'  =>  $token);
            $where = $this->getAdapter()->quoteInto('mail = ?', $mail);
        
            $this->update($data, $where);
        }

        return $token;
    }
    
    public function checkToken($mail,$token) {
        $where = $this->_db->select()
                    ->from($this->_name)
                    ->where('mail = ?', $mail)
                    ->where('auth = ?', $token);
        
        $result = $where->query()->fetchAll();
        
        if (sizeof($result)== 0)
        {
            return -1;
        }else
        {
            return 0;
        }
        
    }

}

